package com.squid.base.config.redis;

import jakarta.annotation.Resource;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.DependsOn;

/**
 * redisson配置
 *
 * @author jinhang1
 * @date 2024/07/23
 */
@ConditionalOnBean(RedisAutoConfiguration.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedissonConfig {
    @Resource
    RedisProperties redisProperties;

    @Bean
    public RedissonClient redissonClient() {
        final String PREFIX="redis://";
        // 创建配置 指定redis地址及节点信息
        Config config = new Config();
        config.useSingleServer()
                .setAddress(PREFIX+redisProperties.getHost()+":"+redisProperties.getPort())
                .setDatabase(redisProperties.getDataBase())
                .setPassword(redisProperties.getPassword());

        // 根据config创建出RedissonClient实例
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}
